package view.pane;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Set;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

import controller.GameState;
import controller.human.Action;
import controller.human.User;

import view.Gui;

@SuppressWarnings("serial")
public class VotingPane extends JPanel {
	Gui gui;
	User actor;
	User target;
	Action action;
	
	public VotingPane(Gui gui) {
		this.gui = gui;
		votingPane(1);
	}
	
	public VotingPane(Gui gui, User actor) {
		this.gui = gui;
		this.actor = actor;
		votingPane(2);
	} 
	
	
	public void votingPane(int state) {
		JPanel statePanel = new JPanel();
		
		if (state == 1) {
			statePanel.add(new JLabel("Select your user name"));
		} else  {
			statePanel.add(new JLabel("Select whom you vote"));
		}
		
		Set<User> users = gui.getUsers();
		
		JPanel usersPanel = new JPanel(); 
		usersPanel.setLayout(new GridLayout((users.size()+2)/3,3));
		
		for (User user: users) {
			JButton selectButton = new JButton(user.getName());
			if ((state==1 && user.isDisplay()) || (state==2 && user.isProposed())) {
				
				if (state == 1) {
					selectButton.addActionListener(new LoginActionListener(user));
				} else {
					selectButton.addActionListener(new VoteActionListener(user));
				}
				
				usersPanel.add(selectButton);
			} else {
				selectButton.setEnabled(false);
				usersPanel.add(selectButton);
			}
		}
		
		JPanel others = new JPanel();
		others.add(new JButton(new AbstractAction("Finish") {
			public void actionPerformed(ActionEvent e) {
				gui.changeState(GameState.VoteFinished);
			}
		}));
		others.add(new JButton("Some more button"));
		
		this.setLayout(new GridLayout(3,1));
		this.add(statePanel);
		this.add(usersPanel);
		this.add(others);
	}

	private class LoginActionListener implements ActionListener {
		User user;
		LoginActionListener(User user) {
			this.user = user;
		}
		
		public void actionPerformed(ActionEvent e) {
			JPasswordField password = new JPasswordField();
			final JComponent[] inputs = new JComponent[] {
			                new JLabel("Password"),
			                password
			};
			JOptionPane.showMessageDialog(null, inputs, "Please enter password", JOptionPane.PLAIN_MESSAGE);
			System.out.println("Pass " + (password.getText()));
			if (user.getPassword()==null || 
					user.getPassword().equals((password.getText()))) {
				gui.updatePane(new VotingPane(gui, user));				
			}
		}
	}
	
	private class VoteActionListener implements ActionListener {
		User user;
		VoteActionListener(User user) {
			this.user = user;
		}
		
		public void actionPerformed(ActionEvent e) {
			if(JOptionPane.showConfirmDialog(null, "Selected " + user.getName(), 
					"Confirmation", JOptionPane.OK_CANCEL_OPTION) == 0) {
				gui.addVote(actor, user);
				gui.updatePane(new VotingPane(gui));	
			}  else {
				// nothing
			}  			
		}
	}
}
